package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.IndexSearchListBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinda Yang on 2017/12/8.
 * Description : 全局搜索结果
 */

public class SearchResultAdapter extends BaseRecyclerViewAdapter<IndexSearchListBean.DataBean.ResultBean> {

    private Context mContext;
    private OnItemClickListener mOnItemClickListener;

    public SearchResultAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(mContext, R.layout.item_search_result, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        IndexSearchListBean.DataBean.ResultBean bean = mList.get(position);
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).setData(bean);
            ((ViewHolder) holder).setListener(bean);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_result_type)
        TextView tvResultType;
        @BindView(R.id.tv_result)
        TextView tvTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(IndexSearchListBean.DataBean.ResultBean bean) {
            switch (bean.getIsNum()) {
                case "2":
                    tvResultType.setText("活动");
                    break;
                case "3":
                    tvResultType.setText("发现");
                    break;
                case "4":
                    tvResultType.setText("兴趣");
                    break;
            }
            tvTitle.setText(bean.getTitle());
        }

        public void setListener(final IndexSearchListBean.DataBean.ResultBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(itemView, getAdapterPosition(), bean);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, IndexSearchListBean.DataBean.ResultBean bean);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
